<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="变更时间">
				<el-date-picker v-model="dateRange1" type="datetimerange" :picker-options="pickerOptions" range-separator="至"
				 start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="条形码" prop="barCode">
				<el-input v-model="queryParams.barCode" placeholder="请输入条形码" clearable
				 @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="资产大类" prop="equipCateg">
				<el-select v-model="queryParams.equipCateg" placeholder="请选择资产大类" clearable>
					<el-option v-for="dict in dict.type.md_assets_category" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['meter:mdStoreLocChg:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="mdStoreLocChgList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="变更记录标识" align="center" prop="chgId" />
			<el-table-column label="原储位编号" align="center" prop="srcStoreLocNo" />
			<el-table-column label="原储位名称" align="center" prop="srcStoreLocName" />
			<el-table-column label="新储位编号" align="center" prop="storeLocNo" />
			<el-table-column label="新储位名称" align="center" prop="storeLocName" />
			<el-table-column label="变更时间" align="center" prop="chgDate" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.chgDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="条形码" align="center" prop="barCode" />
			<el-table-column label="资产大类" align="center" prop="equipCateg">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.md_assets_category" :value="scope.row.equipCateg" />
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 添加或修改储位变更记录对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="库房标识,库房标识" prop="whId">
					<el-input v-model="form.whId" placeholder="请输入库房标识,库房标识" />
				</el-form-item>
				<el-form-item label="库区标识" prop="whAreaId">
					<el-input v-model="form.whAreaId" placeholder="请输入库区标识" />
				</el-form-item>
				<el-form-item label="存放区标识" prop="storeAreaId">
					<el-input v-model="form.storeAreaId" placeholder="请输入存放区标识" />
				</el-form-item>
				<el-form-item label="原储位标识" prop="srcStoreLocId">
					<el-input v-model="form.srcStoreLocId" placeholder="请输入原储位标识" />
				</el-form-item>
				<el-form-item label="新储位标识" prop="storeLocId">
					<el-input v-model="form.storeLocId" placeholder="请输入新储位标识" />
				</el-form-item>
				<el-form-item label="变更时间" prop="chgDate">
					<el-date-picker clearable v-model="form.chgDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择变更时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="条形码" prop="barCode">
					<el-input v-model="form.barCode" placeholder="请输入条形码" />
				</el-form-item>
				<el-form-item label="设备类别" prop="equipCateg">
					<el-input v-model="form.equipCateg" placeholder="请输入设备类别" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listMdStoreLocChg,
		getMdStoreLocChg,
		delMdStoreLocChg,
		addMdStoreLocChg,
		updateMdStoreLocChg
	} from "@/api/meter/mdStoreLocChg";

	export default {
		name: "MdStoreLocChg",
		dicts: ["md_assets_category"],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 日期范围
				dateRange1: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 储位变更记录表格数据
				mdStoreLocChgList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					whId: null,
					whAreaId: null,
					storeAreaId: null,
					srcStoreLocId: null,
					storeLocId: null,
					chgDate: null,
					barCode: null,
					equipCateg: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {},
				pickerOptions: {
					shortcuts: [{
						text: '最近一周',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近一个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近三个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
							picker.$emit('pick', [start, end]);
						}
					}]
				},
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询储位变更记录列表 */
			getList() {
				this.loading = true;
				listMdStoreLocChg(this.addDateRange(this.queryParams, this.dateRange1)).then(response => {
					this.mdStoreLocChgList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					chgId: null,
					whId: null,
					whAreaId: null,
					storeAreaId: null,
					srcStoreLocId: null,
					storeLocId: null,
					chgDate: null,
					barCode: null,
					equipCateg: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.dateRange1 = [];
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.chgId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加储位变更记录";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const chgId = row.chgId || this.ids
				getMdStoreLocChg(chgId).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改储位变更记录";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.chgId != null) {
							updateMdStoreLocChg(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addMdStoreLocChg(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const chgIds = row.chgId || this.ids;
				this.$modal.confirm('是否确认删除储位变更记录编号为"' + chgIds + '"的数据项？').then(function() {
					return delMdStoreLocChg(chgIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('meter/mdStoreLocChg/export', {
					...this.queryParams
				}, `储位变更_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
